package com.myproject.thread.pool;

import org.junit.Test;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*
* * 一秒钟添加十个任务，核心线程数是10个，队列长度为100，跑2秒再关闭，核心线程中有10个，队列中有10个
* * 实测，队列中10个丢弃
* * shutdownnow执行完当前的任务，丢弃队列中的任务，并且拒绝添加
* */
public class Test14 {

    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(100));
    public static void main(String[] args) throws InterruptedException {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    executor.execute(new Task());
                }
            }
        };
        new Thread(runnable).start();
        Thread.sleep(2000);
        executor.shutdownNow();

    }

    static class Task implements Runnable{

        @Override
        public void run() {
            for (int i = 1; i < Integer.MAX_VALUE; i++) {
                if (i%100000000==0){
                    System.out.println(Thread.currentThread().getName()+"---"+i);

                }
            }
        }
    }

    @Test
    public void test1(){
        System.out.println(Integer.MAX_VALUE);
        for (int i = 1; i < Integer.MAX_VALUE; i++) {
            if (i%100000000==0){
                System.out.println(Thread.currentThread().getName()+"--"+i);

            }
        }
    }

}
